/*
 * Copyright 2009 André Luiz Alves Moraes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * */



package org.alamoraes.codegen;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import org.alamoraes.sfactory.ObjectFactory;

/**
 * Runs over a file to load the safety marks and keeps the marks for future use
 * @author andre
 */
public class CodeSafetyMarksLoader extends CodeFile {

    private Hashtable<String, List<String>> marks;

    public CodeSafetyMarksLoader() {
        marks = new Hashtable<String, List<String>>(10, 0.7f);
    }

    /**
     * Get the marks table
     * @return
     */
    public Hashtable<String, List<String>> getMarks() {
        return marks;
    }

    /**
     * Parse the stream looking for code marks
     * @param is
     */
    public void parseStream(InputStream is) throws IOException {
        prepareParse();
        IStreamReaderWriterBuilder srwb =AmCodeGenConsts.getObjectFactory().getInstance(IStreamReaderWriterBuilder.class);
        BufferedReader br = new BufferedReader(srwb.getReader(is),1024);
        String line = null;
        while((line = br.readLine()) != null) {
            if (isLineBlockOpenTag(line)) {
                String blockName = getBlockName(line);
                Vector<String> lines = new Vector<String>();
                // fetch the block until it finds the end block or the end of document
                // all lines are converted to the line set by ISystemProperties
                readUntilEndUserBlock(br, lines);
                
                getMarks().put(blockName, lines);
            }
        }
    }

    /**
     * Helper method to parse a file
     * @param f target file
     * @throws IOException
     */
    public void parseFile(File f) throws IOException {
        parseStream(new FileInputStream(f));
    }

    /**
     * Consumes the reader util it finds the endUserBlockTag
     * @return
     */
    protected void readUntilEndUserBlock(BufferedReader br, List<String> codeLines) throws IOException {
        String line = null;
        while((line = br.readLine()) != null) {
            if (!isLineBlockCloseTag(line)) {
                codeLines.add(line);
            } else {
                break;
            }
        }
    }

    /**
     * Called before starting the parse process.
     *
     * Clear the mark table
     */
    protected void prepareParse() {
        getMarks().clear();
    }
}
